package com.hq.myapplicationdemo.views.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hq.myapplicationdemo.R;




public class FreeRecyclerView extends RecyclerView {

    private int firstVisibleItemPosition = 0;
    private int lastVisibleItemPosition = 0;
    private int offset = 0;
    private View mCurrentHeader;


    public FreeRecyclerView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        addScrollListener();
    }

    public FreeRecyclerView(Context context) {
        super(context);

        addScrollListener();
    }

    @Override
    public void addOnScrollListener(OnScrollListener listener) {
        super.addOnScrollListener(listener);
    }

    private void addScrollListener() {
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                scrollBy(offset);

                //mCurrentHeader = getHeaderView(firstVisibleItemPosition, mCurrentHeader);

            }
        });
    }

    public void setmCurrentHeader(View mCurrentHeader) {
        this.mCurrentHeader = mCurrentHeader;
    }

    //    private View getHeaderView(int position, View v) {
//
//        View view = adapter.getHeaderView(position);
//        if (view != null) {
//            v = view;
//        }
//        v.setFocusable(false);
//        return v;
//    }

    public View getmCurrentHeader() {
        return mCurrentHeader;
    }

    /**
     * 获得屏幕内的Position
     */
    private void findEyeablePosition() {

        LayoutManager layoutManager = getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        }
    }

    /**
     * 当某个item滚动的时候，屏幕内的其他item跟着滚动
     *
     * @param moveX
     */
    public void scrollTo(int moveX) {
        this.offset = moveX;
        findEyeablePosition();

        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
            try {
                startAnmateView(getAnimateView(i), moveX);
            } catch (Exception e) {
              //  LogUtils.i("快速滑动遍历异常");
            }

        }
       startAnmateView(findAnimateScrollView(mCurrentHeader), offset);//mCurrentHeader scrollView move
        invalidate();
    }

    private void startAnmateView(AnimateScrollView scrollView, int moveX) {
        scrollView.smoothScrollTo(moveX, 0);
    }


    /**
     * 滚动的时候刚进入屏幕内的item 增加一个偏移量 offset
     *
     * @param offset
     */
    private void scrollBy(int offset) {
        findEyeablePosition();
        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
            startScrollBy(getAnimateView(i), offset);
        }
    }

    private void startScrollBy(AnimateScrollView scrollView, int moveX) {
        scrollView.scrollTo(moveX, 0);
    }


    private AnimateScrollView getAnimateView(int index) {
        View childView = findViewHolderForAdapterPosition(index).itemView;
        AnimateScrollView horizontalScrollView = findAnimateScrollView(childView);
        return horizontalScrollView;
    }


    private AnimateScrollView findAnimateScrollView(View v) {
        return (AnimateScrollView) v.findViewById(R.id.scrollView);
    }









//    public boolean findHeaderPositionUnder(int x, int y) {
//        //判断点击的位置是否header的区域内
//        //mCurrentHeader
//        //int left, int top, int right, int bottom
//        Rect rect = new Rect();
//        mCurrentHeader.getDrawingRect(rect);
//        if (rect.contains(x, y)) {
//            // LogUtils.i("点击： 包含");
//            return true;
//        }
//        return false;
//    }

//    public boolean findHeaderClickView(View view, int x, int y) {
//        if (view == null) return false;
//        Rect rect = new Rect();
//        mCurrentHeader.getDrawingRect(rect);
//        if (rect.contains(x, y)) {
//            Rect vRect = new Rect();
//            // 需要响应点击事件的区域在屏幕上的坐标
//            vRect.set(rect.left + view.getLeft(), rect.top + view.getTop(), rect.left + view.getLeft() + view.getWidth(), rect.top + view.getTop() + view.getHeight());
//            return vRect.contains(x, y);
//        }
//
//        return false;
//    }

//    public boolean findHeaderClickItemView(View parent, View view, int x, int y) {
//        if (view == null) return false;
//        Rect rect = new Rect();
//        mCurrentHeader.getDrawingRect(rect);
//
//        if (rect.contains(x, y)) {
//            Rect vRect = new Rect();
//            // 需要响应点击事件的区域在屏幕上的坐标
//            vRect.set(rect.left + parent.getLeft() + view.getLeft(), rect.top + parent.getTop() + view.getTop(), rect.left + parent.getLeft() + view.getLeft() + view.getWidth(), rect.top + parent.getTop() + view.getTop() + view.getHeight());
//            return vRect.contains(x, y);
//        }
//        return false;
//    }

    /**
     * 滑动 +铺tab判断
     *
     * @param parent
     * @param view
     * @param scrollX
     * @param x
     * @param y
     * @return
     */
//    public boolean findHeaderAddClickItemView(View parent, View view, int scrollX, int x, int y) {
//        if (view == null) return false;
//        Rect rect = new Rect();
//        mCurrentHeader.getDrawingRect(rect);
//
//        if (rect.contains(x, y)) {
//            Rect vRect = new Rect();
//            // 需要响应点击事件的区域在屏幕上的坐标
//            vRect.set(rect.left + parent.getLeft() + view.getLeft() - scrollX, rect.top + parent.getTop() + view.getTop(), rect.left + parent.getLeft() + view.getLeft() + view.getWidth() - scrollX, rect.top + parent.getTop() + view.getTop() + view.getHeight());
//            return vRect.contains(x, y);
//        }
//
//        return false;
//    }

    /**
     * 滑动 铺tab判断
     *
     * @param parent
     * @param view
     * @param chartView
     * @param scrollX
     * @param x
     * @param y
     * @return
     */
//    public boolean findHeaderAbsoluteClickItemView(View parent, View view, View chartView, int scrollX, int x, int y) {
//        if (view == null) return false;
//        Rect rect = new Rect();
//        mCurrentHeader.getDrawingRect(rect);
//
//        if (rect.contains(x, y)) {
//            Rect vRect = new Rect();
//            // 需要响应点击事件的区域在屏幕上的坐标
//            vRect.set(rect.left + parent.getLeft() + view.getLeft() + chartView.getLeft() - scrollX, rect.top + parent.getTop() + view.getTop() + chartView.getTop(), rect.left + parent.getLeft() + view.getLeft() + chartView.getLeft() + chartView.getWidth() - scrollX, rect.top + parent.getTop() + view.getTop() + chartView.getTop() + chartView.getHeight());
//            return vRect.contains(x, y);
//        }
//
//        return false;
//    }
}
